package com.sunda.spmswms.mapper;

import com.sunda.spmswms.entity.SapDeliveryNoteDtl;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * SAP交货单表明细 Mapper 接口
 * </p>
 *
 * @author Wayne
 * @since 2021-03-22
 */
@Mapper
public interface SapDeliveryNoteDtlMapper extends BaseMapper<SapDeliveryNoteDtl> {

    @Select("SELECT DND.DELIVERY_NOTE_SAP AS \"deliveryNoteSap\",\n" +
            "       DND.DELIVERY_NOTE_SAP_ITEM AS \"deliveryNoteSapItem\",\n" +
            "       DND.PURCHASE_GROUP AS \"purchaseGroup\",\n" +
            "       DND.PURCHASE_GROUP_NAME AS \"purchaseGroupName\",\n" +
            "       DND.MATERIAL_NO AS \"materialNo\",\n" +
            "       DND.DELIVERY_QTY AS \"deliveryQty\",\n" +
            "       DND.DELIVERY_UNIT AS \"deliveryUnit\",\n" +
            "       DND.WERKS AS \"werks\",\n" +
            "       DND.WHS_LOCATION_CODE AS \"whsLocationCode\",\n" +
            "       DND.RECEIVED_QTY AS \"receivedQty\",\n" +
            "       SAP.BASIC_UNIT AS \"basicUnit\",\n" +
            "       DND.ITEM_STATUS AS \"itemStatus\",\n" +
            "       DND.OVER_DELIVERY_LIMIT AS \"overDeliveryLimit\",\n" +
            "       DND.UNDER_DELIVERY_LIMIT AS \"underDeliveryLimit\",\n" +
            "       DND.UNLIMITED AS \"unlimited\",\n" +
            "       DND.REMARKS AS \"remarks\",\n" +
            "       DND.TEMP_STORAGE AS \"tempStorage\",\n" +
            "       DND.WERKS_XQ AS \"werksXq\",\n" +
            "       DND.QTY_SALES AS \"qtySales\",\n" +
            "       DND.ACTUAL_QTY_SALES AS \"actualQtySales\",\n" +
            "       SAP.MATERIAL_ZH_DESC AS \"materialZhDesc\",\n" +
            "       SAP.MATERIAL_EN_DESC AS \"materialEnDesc\",\n" +
            "       (CASE WHEN SAP.UNIT_SALES IS NULL THEN SAP.BASIC_UNIT ELSE SAP.UNIT_SALES END) AS \"unitSales\",\n" +
            "       (CASE WHEN SAP.NUMERATOR IS NULL THEN 1 ELSE SAP.NUMERATOR END) AS \"numerator\",\n" +
            "       (CASE WHEN SAP.DENOMINATOR IS NULL THEN 1 ELSE SAP.DENOMINATOR END) AS \"denominator\"\n" +
            "FROM SAP_DELIVERY_NOTE_DTL DND\n" +
            "    LEFT JOIN MATERIAL_SAP SAP ON DND.MATERIAL_NO = SAP.MATERIAL_NO WHERE DND.DELIVERY_NOTE_SAP = #{deliveryNoteSap} ")
    List<Map<String, Object>> sapDeliveryNoteDtls(@Param("deliveryNoteSap") String deliveryNoteSap);

    @Select("SELECT DTL.DELIVERY_NOTE_SAP AS \"deliveryNoteSap\",\n" +
            "       DTL.DELIVERY_NOTE_SAP_ITEM AS \"deliveryNoteSapItem\",\n" +
            "       DTL.MATERIAL_NO AS \"materialNo\",\n" +
            "       DTL.RECEIVED_QTY AS \"receivedQty\",\n" +
            "       DTL.DELIVERY_UNIT AS \"deliveryUnit\",\n" +
            "       SAP.BASIC_UNIT AS \"basicUnit\",\n" +
            "       DTL.WERKS AS \"werks\",\n" +
            "       DTL.WHS_LOCATION_CODE AS \"whsLocationCode\",\n" +
            "       DTL.REMARKS AS \"remarks\"\n" +
            "FROM SAP_DELIVERY_NOTE_DTL DTL\n" +
            "    LEFT JOIN MATERIAL_SAP SAP ON DTL.MATERIAL_NO = SAP.MATERIAL_NO\n" +
            "WHERE DTL.DELIVERY_NOTE_SAP = #{deliveryNoteSap} AND DTL.RECEIVED_QTY > 0 ")
    List<Map<String, Object>> getDeliveryNoteDtlToSap(@Param("deliveryNoteSap") String deliveryNoteSap);
}
